﻿using System;
using System.ComponentModel;
#if SL4BETA
using System.Windows.Interop;
#else
using System.Runtime.InteropServices.Automation;
#endif
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Silverlight.Media.Encoder;
using SilverlightWebcamEncoder.Messages;

namespace SilverlightWebcamEncoder.ViewModels
{
    public class EncoderViewModel : ViewModelBase
    {
        private bool m_isEncoding;
        private int m_encodeFps = 30;
        private string m_encoderProfileData;
        private string m_outputfilename;
        private bool m_networkStream = false;
        private int m_tcpPort = 8090;
        private int m_encodeHeight = 240;
        private int m_encodeWidth = 320;
        private int m_videoBitrate = 500000;
        private string m_installationPath;

        public EncoderViewModel()
        {
            EncodingStopped = new RelayCommand(OnEncodingStopped);
#if SL4BETA
            if (!ComAutomationFactory.IsAvailable)
                return;
#else
            if (!AutomationFactory.IsAvailable)
                return;
#endif
            LoadEncoderProfileData();
            InstallationPath = App.Injector.PayloadExtractionPath;
        }

        private void GenerateOutputFilename()
        {
            OutputFilename = string.Format(@"{0}\{1}.wmv", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Guid.NewGuid());
        }

        private void OnEncodingStopped()
        {
            Messenger.Default.Send<CapturedMediaReady>(new CapturedMediaReady(OutputFilename));
        }

        private void LoadEncoderProfileData()
        {
            string profile = EncoderProfileGenerator.GetProfileAudioVideo(EncodeWidth, EncodeHeight, VideoBitrate);

            EncoderProfileData = profile;
        }

        public string InstallationPath
        {
            get { return m_installationPath; }
            set
            {
                m_installationPath = value;
                RaisePropertyChanged("InstallationPath");
            }
        }

        public ICommand EncodingStopped
        {
            get; private set;
        }

        public int VideoBitrate
        {
            get { return m_videoBitrate; }
            set
            {
                m_videoBitrate = value;
                RaisePropertyChanged("VideoBitrate");
                LoadEncoderProfileData();
            }
        }

        public int EncodeWidth
        {
            get { return m_encodeWidth; }
            set
            {
                m_encodeWidth = value;
                RaisePropertyChanged("EncodeWidth");
                LoadEncoderProfileData();
            }
        }

        public int EncodeHeight
        {
            get { return m_encodeHeight; }
            set
            {
                m_encodeHeight = value;
                RaisePropertyChanged("EncodeHeight");
                LoadEncoderProfileData();
            }
        }

        public bool IsEncoding
        {
            get { return m_isEncoding; }
            set
            {
                if (IsEncoding == value)
                    return;
                
                m_isEncoding = value;

                if (IsEncoding)
                    GenerateOutputFilename();

                RaisePropertyChanged("IsEncoding");

                Messenger.Default.Send<EncoderPrepared>(new EncoderPrepared());
            }
        }

        public int EncodeFps
        {
            get { return m_encodeFps; }
            set
            {
                m_encodeFps = value;
                RaisePropertyChanged("EncodeFps");
            }
        }

        public string EncoderProfileData
        {
            get { return m_encoderProfileData; }
            set
            {
                m_encoderProfileData = value;
                RaisePropertyChanged("EncoderProfileData");
            }
        }

        public string OutputFilename
        {
            get { return m_outputfilename; }
            set
            {
                m_outputfilename = value;
                RaisePropertyChanged("OutputFilename");
            }
        }

        public bool NetworkStream
        {
            get { return m_networkStream; }
            set
            {
                m_networkStream = value;
                RaisePropertyChanged("NetworkStream");
            }
        }

        public int TcpPort
        {
            get { return m_tcpPort; }
            set
            {
                m_tcpPort = value;
                RaisePropertyChanged("TcpPort");
            }
        }
    }
}
